# Disponivel no pacote de programas como: mnet.py
import email, sys
import email.Errors
import mailbox
import networkx as NX
from pylab import show

def msgfactory(fp):
    try:
        return email.message_from_file(fp)
    except email.Errors.MessageParseError:
        return ''

def starG(mess,G):
    try:
    	centro = mess.get('from').split()[-1].strip('<>')
        pontas = [j.split()[-1].strip('<>') for j in mess.get_all('to')[0].split(',')]
        arestas = [(centro,k) for k in pontas]
        G.add_edges_from(arestas)
        return G
    except:
        print'falhou'

dir='/home/flavio/Mail/inbox'
# Veja documentacao do modulo mailbox
# para outros tipo de mailboxes.
mbox = mailbox.Maildir(dir, msgfactory) # mailbox do kmail
G = NX.DiGraph()
for n in xrange(50):
    i = mbox.next()
    starG(i,G)
print G.nodes()
G = NX.convert_node_labels_to_integers(G)
NX.draw(G, width=2, style='dotted', alpha=0.5)
show()
    
